<template>
    <div class="app-container">
        <el-form ref="form" v-loading="loading" element-loading-text="正在上传到服务器" :model="form" :rules="rules" label-width="120px" size="medium">
            <el-form-item label="版本名称" prop="versionName">
                <el-input v-model="form.versionName" style="width:300px" placeholder="请输入版本名称"/>
            </el-form-item>
            <el-form-item label="版本描述" prop="versionDesc">
                <el-input v-model="form.versionDesc" type="textarea" style="width:300px" placeholder="请输入版本描述"/>
            </el-form-item>
            <el-form-item label="APK文件上传">
                <el-upload
                    class="avatar-uploader"
                    :action="action"
                    :show-file-list="false"
                    ref="upload"
                    :headers="httpHeader"
                    :on-success="handleAvatarSuccess"
                    :on-change="handleChange"
                    :data="form"
                    :auto-upload="false">
                    <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                    <el-button
						style="margin-left: 10px;"
						size="small"
						type="success"
						@click="submitUpload">上传到服务器</el-button>
                </el-upload>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { uploadApk } from '@/api/apk.js'
import {baseURL} from '@/utils/baseURL'
import { getToken } from '@/utils/auth' 
export default {
    data() {
        return {
            action: baseURL + '/back/version/uploadApk',
            form: {
                versionName: '',                     //版本名称
                versionDesc: ''                     //版本描述  
            },
			fullscreenLoading: false,
            sendData: {versionName: '', versionDesc: ''},
            httpHeader: {
                'Authorization': ''
            },
            loading: false
        }
    },
    methods: {
        // 文件上传成功钩子函数
        handleAvatarSuccess(response, file, fileList) {
			// this.fullscreenLoading = true
            this.loading = false
			const info = response.msg
			if(response.code == 1) {
				this.$message({
					type: 'success',
					message: info
				})
			} else {
				this.$message({
					message: info,
					type: 'error',
				})
			}
			// this.fullscreenLoading = false
        },
        handleChange(file, fileList) {
            // this.httpHeader.Authorization = this.dealCookie(document.cookie)
            this.httpHeader.Authorization = getToken()
        },
        // cookies的处理函数
        dealCookie(value) {
            const cookieArr = value.split('=')
            return cookieArr[1]
        },
        // 上传到服务器按钮绑定函数
        async submitUpload() {
            this.loading = true
            this.$refs.upload.submit()
        }
    }
}
</script>

<style>

</style>